﻿using System.Data.Entity;
using System;
using System.Collections.Generic;

namespace TraceThisItem.Domain.Initializers
{
	/// <summary>
	/// Esta clase sobreescribe el método Seed, el cual sirve para guardar algunos datos iniciales en el modelo que acaba de ser re-creado.
	/// </summary>
	public class DropCreateAndSeedDatabaseAlways : DropCreateDatabaseAlways<PersistenceManager>
	{
		protected override void Seed(PersistenceManager context)
		{

			//context.Database.ExecuteSqlCommand("ALTER TABLE ItemTypes ADD CONSTRAINT uc_ItemType_WorkflowId UNIQUE(WorkflowId)");

			#region Usuarios

			var userPablo = new User
			{
				Firstname = "Pablo",
				Lastname = "Carballo",
				UserName = "pcarballo",
			};

			var userCT = new User
			{
				Firstname = "Carlos",
				Lastname = "Tacchi",
				UserName = "ctacchi",
			};

			var userHernan = new User
			{
				Firstname = "Hernán",
				Lastname = "Zbucki",
				UserName = "hezbu",
			};

			var userJV = new User
			{
				Firstname = "Juana",
				Lastname = "Vialde",
				UserName = "jvialde",
			};

			context.Users.Add(userPablo);
			context.Users.Add(userHernan);
			context.Users.Add(userCT);
			context.Users.Add(userJV);


			#endregion

			#region Proyectos

			var project1 = new Project
			{
				Name = "Forúnculo S.A.",
				OwnerUserName = "pcarballo",
				CreatedOn = DateTime.Now
			};

			var project2 = new Project
			{
				Name = "Proyecto MOSCA",
				OwnerUserName = "hezbu",
				CreatedOn = DateTime.Now
			};

			context.Projects.Add(project1);
			context.Projects.Add(project2);

			#endregion

			#region Equipos
			var team1 = new Team
				{
					Name = "Grosos"
				};

			team1.Users.Add(userHernan);
			team1.Users.Add(userPablo);

			var team2 = new Team
			{
				Name = "Colaboradores"
			};

			team2.Users.Add(userCT);
			team2.Users.Add(userJV);

			project2.Teams.Add(team2);
			project1.Teams.Add(team1); 
			#endregion

			var wf = new Workflow("Básico");
			context.Workflows.Add(wf);

			var statusNodeInicial = new StatusNode
			{
				Name = "Inicial",
				Workflow = wf

			};

			var statusNodeFinalizado = new StatusNode
			{
				Name = "Finalizado",
				NextStatusNodes = null,
				Workflow = wf

			};

			statusNodeInicial.NextStatusNodes.Add(statusNodeFinalizado);
			context.StatusNodes.Add(statusNodeInicial);
			context.StatusNodes.Add(statusNodeFinalizado);


			var itemTypeBug = new ItemType
			{
				Name = "Bug",
                Project = project1
			};

			var itemTypePerfBug = new ItemType
			{
				Name = "Performance Bug",
				Project = project2
			};
			wf.ItemTypes.Add(itemTypePerfBug);
			wf.ItemTypes.Add(itemTypeBug);
			context.ItemTypes.Add(itemTypeBug);
			context.ItemTypes.Add(itemTypePerfBug);

			var itemHz = new Item
			{
				CreatedOn = System.DateTime.Now,
				Description = "Error de conexión a datos",
                Subject = "Incidencia reportada muy loca",
				LatestUpdate = System.DateTime.Now,
				Priority = (byte)(ItemPriority.Alta),
				Project = project2,
				Team = team1,
				Type = itemTypeBug
			};
			var itemPmc = new Item
			{
				CreatedOn = System.DateTime.Now,
				Description = "Ventana fuera del borde",
                Subject = "Incidencia reportada muy loca",
				LatestUpdate = System.DateTime.Now,
				Priority = (byte)(ItemPriority.Baja),
				Project = project1,
				Team = team1,
				Type = itemTypeBug
			};

			var is1 = new ItemStatus
			{
				Date = System.DateTime.Now,
				Responsible = userPablo,
				StatusNode = statusNodeInicial
			};
			var is2 = new ItemStatus
			{
				Date = System.DateTime.Now.AddHours(-1),
				Responsible = userHernan,
				StatusNode = statusNodeInicial
			};
			context.ItemStatus.Add(is1);
			context.ItemStatus.Add(is2);

			itemHz.Traceability = new List<ItemStatus>();
			itemPmc.Traceability = new List<ItemStatus>();
			itemHz.Traceability.Add(is2);
			itemPmc.Traceability.Add(is1);

			context.Items.Add(itemPmc);
			context.Items.Add(itemHz);

            /*#region Carga de Status Types Demo

            var statusTypeNuevo = new StatusType
                {
                    Name = "Nuevo",
                    Project = project
                };
            context.StatusTypes.Add(statusTypeNuevo);

            context.StatusTypes.Add(new StatusType
            {
                Name = "Asignado",
                Project = project
            });

            context.StatusTypes.Add(new StatusType
            {
                Name = "Se necesitan más datos",
                Project = project
            });

            context.StatusTypes.Add(new StatusType
            {
                Name = "Resuelto",
                Project = project
            });

            context.StatusTypes.Add(new StatusType
            {
                Name = "Cerrado",
                Project = project
            });

            #endregion Carga de Status Types Demo

			context.Items.Add(new Item
			{
				CreatedOn = DateTime.Now,
				CurrentStatus = statusTypeNuevo,
				Description = "Error muy feo",
				LatestUpdate = DateTime.Now,
				Priority = (byte)ItemPriority.Alta,
				Project = project,
				Responsible = userPablo,
				Team = teamGrosos,
				Type = itemTypeBug
			});

			context.Items.Add(new Item
			{
				CreatedOn = DateTime.Now,
				CurrentStatus = statusTypeNuevo,
				Description = "Otro error muy feo",
				LatestUpdate = DateTime.Now,
				Priority = (byte)ItemPriority.Alta,
				Project = project,
				Responsible = userPablo,
				Team = teamGrosos,
				Type = itemTypeBug
			});*/
		}
	}

  
}